package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private IYddCateService iYddCateService;

    @Autowired
    private IYddArticleService iYddArticleService;

    @Autowired
    private IYddAdvimgService iYddAdvimgService;

    @Autowired
    private IYddAdvposService iYddAdvposService;

    @Autowired
    private IYddTagService iYddTagService;

    @Autowired
    private IYddUserService iYddUserService;

    @Override
    public R profile(Long userId) {
        //1.1 查询登录用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //1.2 填入用户角色信息
        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        String sysUserRoleId = sysUser.getRoleId();
        sysRoleWrapper.eq(SysRole::getId,sysUserRoleId.substring(2, sysUserRoleId.length() - 2));
//        sysRoleWrapper.select(SysRole::getName);
        //1.2.1 查出对应的角色详细信息
        SysRole sysRole = sysRoleService.getOne(sysRoleWrapper);
        //1.2.2 封装进sysUserDto对象中
        SysUserDto userInfo = new SysUserDto();
        BeanUtils.copyProperties(sysUser,userInfo);
        userInfo.setRolename(Collections.singletonList(sysRole.getName()));

        //2 查菜单数据
        LambdaQueryWrapper<SysMenu> sysMenuWrapper = new LambdaQueryWrapper<>();
        sysMenuWrapper.ne(SysMenu::getPath,"");
        List<SysMenu> menuList = sysMenuService.list(sysMenuWrapper);

        //3 dictsList
        LambdaQueryWrapper<SysDict> sysDictWrapper = new LambdaQueryWrapper<>();
        sysDictWrapper.select(SysDict::getType);
        sysDictWrapper.groupBy(SysDict::getType);
        //3.1 分组查询所有dict_type
        List<Object> dictTypes = sysDictService.listObjs(sysDictWrapper);
        Map<String, Map<String,String>> dictsList = new HashMap<>();
        //3.2 遍历dictTypes ，查询对应的dict信息，存入dictsList中
        for (Object o : dictTypes) {
            Map<String, String> map = new HashMap<>();

            sysDictWrapper = new LambdaQueryWrapper<>();
            sysDictWrapper.eq(SysDict::getType,o);
            List<SysDict> sysDicts = sysDictService.list(sysDictWrapper);
            for (SysDict sysDict : sysDicts) {
                //3.3 将dict_value,dict_label存入集合
                map.put(sysDict.getValue(),sysDict.getLabel());
            }

            //3.4 将保存后的数据存入dictsList
            dictsList.put((String)o,map);
        }

        //4 准备 btnList
        Map<Integer,String> btnList = new HashMap<>();
        btnList.put(0,"*");

        //5 将所有查出的数据存入 R 中，返回
        R r = new R();
        r.putData("btnList",btnList);
        r.putData("dictsList",dictsList);
        r.putData("menuList",menuList);
        r.putData("userInfo",userInfo);

        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }

    public Map<String, Object> baseInfo() {
        //所有集合的总和
        HashMap<String, Object> maps = new HashMap<>();

        //1.查询所有分类并添加到集合
        List<YddCate> cateList = iYddCateService.list();
        //需要的数据集合
        ArrayList<Map<String, Object>> cates = new ArrayList<>();

        for (YddCate yddCate : cateList) {
            //需要的数据map
            HashMap<String, Object> cate = new HashMap<>();
            //根据分类id查询当前分类下文章总数
            Integer cateId = yddCate.getId();
            LambdaQueryWrapper<YddArticle> qw = new LambdaQueryWrapper<>();
            qw.eq(YddArticle::getCateid, cateId);
            int count = iYddArticleService.count(qw);
            cate.put("id", yddCate.getId());
            cate.put("catename", yddCate.getCatename());
            cate.put("num", count);

            //将分类信息map 放到分类信息集合
            cates.add(cate);
        }
        maps.put("all_cate", cates);

        // 2.统计广告位总数
        int AdvposCount = iYddAdvposService.count();
        maps.put("advpos_num", AdvposCount);

        // 3. 标签总数
        int tagCount = iYddTagService.count();
        maps.put("tag_num", tagCount);

        // 4.管理员总数
        int sysUserCount = sysUserService.count();
        maps.put("admin_num", sysUserCount);

        // 5.文章总数
        int ArticleCount = iYddArticleService.count();
        maps.put("article_num", ArticleCount);

        // 6.分类总数
        maps.put("cate_num", cateList.size());

        // 7.广告图总数
        int AdvimgCount = iYddAdvimgService.count();
        maps.put("advimg_num", AdvimgCount);

        // 8.最新的用户数据 根据创建时间排序
        Page<YddUser> yddUserPage = new Page<>();
        yddUserPage.setSize(7);
        LambdaQueryWrapper<YddUser> yuqw = new LambdaQueryWrapper<>();
        yuqw.orderByDesc(YddUser::getId);
        iYddUserService.page(yddUserPage, yuqw);
        maps.put("new_user", yddUserPage.getRecords());

        // 9.查询热门文章数据 降序
        LambdaQueryWrapper<YddArticle> artqw = new LambdaQueryWrapper<>();
        artqw.orderByDesc(YddArticle::getClick);
        List<YddArticle> ArtList = iYddArticleService.list(artqw);
        maps.put("hot_article", ArtList);


        // 10.统计用户信息
        HashMap<String, Object> userInfo = new HashMap<>();
        int userCount = iYddUserService.count();
        LambdaQueryWrapper<YddUser> userqw = new LambdaQueryWrapper<>();
        userqw.eq(YddUser::getSex, 0);
        int manCount = iYddUserService.count(userqw);
        userInfo.put("total", userCount);
        userInfo.put("user_man", manCount);
        userInfo.put("user_woman", (userCount-manCount));

        maps.put("user", userInfo);


        return maps;
    }
}
